package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.example.common.dto.Result;
import org.example.common.dto.ResultUtil;
import org.example.service.SwitchService;
import org.example.service.mqtt.MqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 李荣
 * @Date 2025/1/4
 * @Description
 */

@Service
@Slf4j
public class SwitchServiceImpl implements SwitchService {
    private final String sendStatusTopic = "send-switch-status";
    @Autowired
    private MqttService mqttService;

    @Override
    public Result<?> updateService(Integer status) {
        try {
            mqttService.publish("send-switch-status",new MqttMessage(String.valueOf(status).getBytes()));
        } catch (MqttException e) {
            log.info("开关的打开或者关闭有问题,请稍后重试或者联系管理员");
            e.printStackTrace();
        }
        return ResultUtil.success("","开关的状态改变");
    }
}
